package gui.properties.groups;

import java.awt.Color;
import java.io.File;
import java.util.ArrayList;
import java.util.Scanner;

import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;

import model.Model;

import util.reflection.MethodAction;
import util.reflection.Reflection;
import controller.AbstractController;
import controller.PropertiesController;
import controller.SLogoController;
/**
 * @author Shun Fan
 * 
 */
public class FieldGroup extends AbstractGroup {
	private JTextField myField;
	private String receiveMethodName;
	private PropertiesController myController;
	
	private static final String SCANNER_DELIMITTER = ";\\s*";	
	
	public FieldGroup(Model model, PropertiesController controller, String parameterLine) {
		System.out.println("Field Group Starting");
		myController = controller;
		
		Scanner reader = new Scanner(parameterLine).useDelimiter(SCANNER_DELIMITTER);
		String labelName = reader.next();
		String sendMethodName = reader.next();
		receiveMethodName = reader.next();
		int fieldLength = Integer.parseInt(reader.next());
		
		myField = new JTextField(fieldLength);
		JButton button = new JButton("Set");		
		button.addActionListener(
				new MethodAction(controller, 
				sendMethodName, model, myField)	
		);
		
		add(new JLabel(labelName));
		add(myField);
		add(button);
	}

	@Override
	public void update() {
		System.out.println("fieldgroup updating: " + receiveMethodName);
		Reflection.callMethod(myController, receiveMethodName, myField);	
	}
	
	/*
	public static void main(String args[]) {
		Model model = new Model();
		SLogoController controller = new SLogoController(model);
		PropertiesController prop = new PropertiesController(model);
		Properties p = new Properties("src/resources/properties/test.txt", model, controller);
		JFrame f = new JFrame();
		f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		p.setBackground(Color.CYAN);
		//f.setLayout(new FlowLayout());
		
		
		ComboBoxGroup c = new ComboBoxGroup(model, prop, "; Visibility; setVisibility; visible; invisible;");
		f.add(c);
		f.setSize(1000,500);
		f.setVisible(true);
	}*/
}
